package com.siashan.toolkit.oss.aliyun.core;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.siashan.toolkit.oss.core.OssResult;
import com.siashan.toolkit.oss.core.OssStorageService;
import com.siashan.toolkit.oss.core.enums.OssChannel;
import com.siashan.toolkit.oss.core.exception.ToolkitOssException;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * 阿里云存储
 *
 * @author siashan
 * @since v1.0.1
 */
public class AliyunOssStorageService extends OssStorageService {
    /**
     * 阿里云OSS客户端
     */
    private OSS client;
    /**
     * 阿里云OSS配置
     */
    private AliyunOssConfig config;


    /**
     * 默认构造方法
     *
     * 初始化阿里云配置
     */
    public AliyunOssStorageService() {
        config = new AliyunOssConfig();
        config.setAccessKeyId(AliyunOssConfiguration.getAccessKeyId());
        config.setAccessKeySecret(AliyunOssConfiguration.getAccessKeySecret());
        config.setBucketName(AliyunOssConfiguration.getBucketName());
        config.setDomain(AliyunOssConfiguration.getDomain());
        config.setEndPoint(AliyunOssConfiguration.getEndPoint());
        config.setPrefix(AliyunOssConfiguration.getPrefix());
        init();
    }

    /**
     * 构造方法
     *
     * @param config  阿里云配置
     */
    public AliyunOssStorageService(AliyunOssConfig config) {
        this.config = config;
        init();
    }

    /**
     * 初始化阿里云客户端
     */
    private void init() {
//        client = new OSSClient(config.getEndPoint(), config.getAccessKeyId(), config.getAccessKeySecret());
        client = new OSSClientBuilder().build(config.getEndPoint(), config.getAccessKeyId(), config.getAccessKeySecret());
    }

    /**
     * 文件上传
     *
     * @param data    文件字节数组
     * @param path    文件路径，包含文件名
     * @return Oss上传结果
     */
    @Override
    public OssResult upload(byte[] data, String path) {
        return upload(new ByteArrayInputStream(data), path);
    }

    /**
     * 文件上传
     *
     * @param inputStream   字节流
     * @param path          文件路径，包含文件名
     * @return   Oss上传结果
     */
    @Override
    public OssResult upload(InputStream inputStream, String path) {
        try {
            client.putObject(config.getBucketName(), path, inputStream);
        } catch (Exception e) {
            throw new ToolkitOssException("上传文件失败，请检查配置信息", e);
        }

        return OssResult.build(path,config.getDomain(), OssChannel.ALIYUN);
    }

    /**
     * 文件上传
     *
     * @param data     文件字节数组
     * @param suffix   文件后缀  eg: .gif .txt .png
     * @return  Oss上传结果
     */
    @Override
    public OssResult uploadSuffix(byte[] data, String suffix) {
        return upload(data, getPath(config.getPrefix(), suffix));
    }

    /**
     * 文件上传
     *
     * @param inputStream  字节流
     * @param suffix       后缀
     * @return  Oss上传结果
     */
    @Override
    public OssResult uploadSuffix(InputStream inputStream, String suffix) {
        return upload(inputStream, getPath(config.getPrefix(), suffix));
    }

    /**
     * 文件删除
     *
     * @param fileName  资源名称
     * @return 是否成功标识
     * @since 1.0.2
     */
    @Override
    public boolean remove(String fileName) {
        client.deleteObject(config.getBucketName(),fileName);
        return true;
    }
}
